package com.MysqlData;

/**
 * @author Xue ShuWen
 * @date 2023/6/19 10:45
 */

import com.google.gson.Gson;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Scanner;
import com.bean.*;

public  class PieData {
    static String driver = "com.mysql.cj.jdbc.Driver";
    static String    url = "jdbc:mysql://localhost:3306/stuInfo";
    static String   root = "root";
    static String     ps = "root";

    Connection coon = null;
    Statement st = null;
    ResultSet rs = null;


    public String queryPie() throws ClassNotFoundException {
        Class.forName(driver);
        String sql = "select * from genderNum order by classNo";

        try {
            coon = DriverManager.getConnection(url,root,ps);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ArrayList<Gender> boys = new ArrayList<Gender>();
        try {
            st = coon.createStatement();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()){
                Gender gender = new Gender();
                gender.setname(rs.getString(1));
                gender.setvalue(rs.getInt(2));
                boys.add(gender);
                System.out.println(gender);
//                System.out.println("用户名是"+rs.getString("uname"));
            }


        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "{\"data\":"+new Gson().toJson(boys)+"}";

    }
    public static void main(String[]args) throws ClassNotFoundException, SQLException {
        Scanner input = new Scanner(System.in);

        new PieData().queryPie();

    }
}













